<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="区域" prop="deptId">
        <el-select
          v-model="queryParams.deptId"
          placeholder="请选择区域"
          clearable
          filterable
          @change="handleSelectDept"
        >
          <el-option
            v-for="dep in deptList"
            :key="dep.deptId"
            :label="dep.deptName"
            :value="dep.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="网关" prop="wardId">
        <el-select
          v-model="queryParams.wardId"
          placeholder="请选择网关"
          clearable
          filterable
        >
          <el-option
            v-for="dep in wardList"
            :key="dep.wardId"
            :label="dep.wardName"
            :value="dep.wardId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="通道名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入通道名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备端口" prop="bedName">
        <el-input
          v-model="queryParams.bedName"
          placeholder="请输入设备端口"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bed:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:bed:edit']"
          >修改</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bed:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:bed:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="bedList"
      @selection-change="handleSelectionChange"
      :default-sort="defaultSort" @sort-change="handleSortChange"
    >
      <el-table-column label="序号" align="center"  type="index" :index="indexMethod">

      </el-table-column>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="自增id" align="center" prop="bedId" /> -->
      <el-table-column label="区域" align="center" prop="deptName" />
      <el-table-column label="网关" align="center" prop="wardName" />
      <el-table-column label="通道名称" align="center" prop="roomName" />
      <el-table-column label="设备端口" align="center" prop="bedName" 
       sortable="custom" :sort-orders="['descending', 'ascending']" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bed:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bed:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备端口对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="区域" prop="deptId">
          <el-select
            :style="{ width: '100%' }"
            v-model="form.deptId"
            placeholder="请选择区域"
            clearable
            filterable
            @change="handleChangeDept2"
          >
            <el-option
              v-for="dep in deptList"
              :key="dep.deptId"
              :label="dep.deptName"
              :value="dep.deptId"
            />
          </el-select>
          <!-- <el-input v-model="form.wardId" placeholder="请输入网关" /> -->
        </el-form-item>
        <el-form-item label="网关" prop="wardId">
          <el-select
            :style="{ width: '100%' }"
            v-model="form.wardId"
            placeholder="请选择网关"
            clearable
            filterable
          >
            <el-option
              v-for="dep in wardList2"
              :key="dep.wardId"
              :label="dep.wardName"
              :value="dep.wardId"
            />
          </el-select>
          <!-- <el-input v-model="form.wardId" placeholder="请输入网关" /> -->
        </el-form-item>
        <el-form-item label="通道名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入通道名称" />
        </el-form-item>
        <el-form-item label="设备端口" prop="bedName">
          <el-input v-model="form.bedName" placeholder="请输入设备端口" />
        </el-form-item>
        <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWard } from "@/api/system/ward";
import { listDepts } from "@/api/system/depts";
import { listBed, getBed, delBed, addBed, updateBed } from "@/api/system/bed";

export default {
  name: "Bed",
  data() {
    return {

      defaultSort: {prop: 'create_time', order: 'descending'},
      // 查询参数
      queryDeptParams: {
        pageNum: 1,
        pageSize: 10000,
        building: null,
        floor: null,
        deptName: null,
      },
      deptList: [],
      // 查询参数
      queryWardParams: {
        pageNum: 1,
        pageSize: 10000,
        deptId: null,
        wardName: null,
      },
      // 查询参数
      queryWardParams2: {
        pageNum: 1,
        pageSize: 10000,
        deptId: null,
        wardName: null,
      },
      wardList: [],
      wardList2: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备端口表格数据
      bedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wardId: null,
        roomName: null,
        bedName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [{ required: true, message: "区域为必填项", trigger: "blur" }],
        wardId: [{ required: true, message: "网关为必填项", trigger: "blur" }],
        roomName: [
          { required: true, message: "通道名称为必填项", trigger: "blur" },
        ],
        bedName: [{ required: true, message: "设备端口为必填项", trigger: "blur" }],
      },
    };
  },
  async created() {
    this.getList();
    this.getDeptList();
    const res = await this.getWardList(this.queryWardParams);
    // 初始化网关
    this.wardList = res.rows;
    this.wardList2 = res.rows;
  },
  methods: {
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
indexMethod(index) {

return index + 1 + (this.queryParams.pageNum- 1) * this.queryParams.pageSize

  },
    async handleChangeDept2(e) {
      this.queryWardParams2.deptId = e;
      const res = await this.getWardList(this.queryWardParams2);
      this.wardList2 = res.rows;
      this.form.wardId = null;
    },
    async handleSelectDept(e) {
      this.queryWardParams.deptId = e;
      const res = await this.getWardList(this.queryWardParams);
      this.wardList = res.rows;
    },
    /** 查询区域列表 */
    getDeptList() {
      this.loading = true;
      listDepts(this.queryDeptParams).then((response) => {
        this.deptList = response.rows;
        this.loading = false;
      });
    },
    /** 查询网关列表 */
    async getWardList(queryWardParams) {
      this.loading = true;
      const res = await listWard(queryWardParams);
      this.loading = false;
      return res;
    },
    /** 查询设备端口列表 */
    getList() {
      this.loading = true;
      listBed(this.queryParams).then((response) => {
        this.bedList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bedId: null,
        wardId: null,
        roomName: null,
        bedName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bedId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备端口";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bedId = row.bedId || this.ids;
      getBed(bedId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备端口";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bedId != null) {
            updateBed(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBed(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bedIds = row.bedId || this.ids;
      this.$modal
        .confirm('是否确认删除设备端口编号为"' + row.bedName + '"的数据项？')
        .then(function () {
          return delBed(bedIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/bed/export",
        {
          ...this.queryParams,
        },
        `bed_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
